package kolejka;

import item.Obiekt;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedList;

public class KolejkaFIFO<T extends Obiekt> implements Kolejka<T> {

	private LinkedList<T> waitingItems;
	private int id;
	

	public KolejkaFIFO(int id) {
		this.id = id;
		this.waitingItems = new LinkedList<T>();
	}

	@Override
	public void dodaj(T item) {
		this.waitingItems.add(item);
//		System.out.println("* +K"+this.id+ " "+item.dajID());
		boolean disabled = false;
		
			if (!disabled) {
				double time = roundTime(item.dajCzasPrzybycia());
				StringBuilder komunikat = new StringBuilder(new String(""));
				komunikat.append("+K"+id);
				komunikat.append(" "+item.dajID());
				komunikat.append("\t czas:" + time + "\t");
				komunikat.append("\t[" + dajItemyWSystemie(waitingItems) + "] rozmiar:"+waitingItems.size());
				
//				System.out.println(komunikat);
			}
		
	
	}
	
	protected double roundTime(double time) {
		time = new BigDecimal(time).round(new MathContext(6)).doubleValue();
		return time;
	}
	
	protected String dajItemyWSystemie(LinkedList<T> queue) {
		StringBuilder itemyWSystemie = new StringBuilder("");
		if (queue.size() > 0) {
			for (T item : queue) {
//				System.out.print(" " + item.dajID());
				itemyWSystemie.append(" " + item.dajID());
			}
		}
		return itemyWSystemie.toString();
	}

	public boolean czyPusty() {
		// TODO Auto-generated method stub
		return this.waitingItems.isEmpty();
	}

	public T dajPierwszy() {
		return this.waitingItems.getFirst();
	}

	public T usunPierwszy() {
		return this.waitingItems.removeFirst();
	}

	@Override
	public int rozmiar() {
		return this.waitingItems.size();
	}

	@Override
	public LinkedList<Obiekt> dajWszystkie() {
		return (LinkedList<Obiekt>) this.waitingItems;
	}
	
	

}
